<?php
if ($_POST) {
    header("Content-type:text/html;charset=utf-8");
    include '../../api/config.php';
    include './comm.php';
    //数据库配置文件
    $json = $_POST["data"];
    $data = json_decode($json);
    $id = $data->head;

    if ($id == '1022') {
        $ids = $data->ids;
        $sql4 = "DELETE FROM ad WHERE id = '{$ids}'";//删除广告
        if(flag($data)){
            $stmt4 = $conn->prepare($sql4);
            $stmt4->execute();
            echo get_msg('200','操作成功~',false,false);
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };
    }// 删除广告

    if ($id == '1021') {
        $name = $data->name;
        $href = $data->href;
        $start_time = $data->start_time;
        $over_time = $data->over_time;
        $type = $data->type;
        $flag1 = $data->flag1;
        $ad_img = $data->ad_img;
        $beizhu = $data->beizhu;
        $sql3 = "INSERT INTO ad(name,href,start_time,over_time,type,flag,ad_img,beizhu) VALUES('{$name}','{$href}','{$start_time}','{$over_time}','{$type}','{$flag1}','{$ad_img}','{$beizhu}')";
        //插入广告
        if(flag($data)){
            $arr2 = array();
            $stmt3 = $conn->prepare($sql3);
            $stmt3->execute();
            echo get_msg('200','操作成功~',false,false);
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };
    }// 插入广告

    if ($id == '1020') {
        $ids = $data->Id;
        $name = $data->name;
        $href = $data->href;
        $start_time = $data->start_time;
        $over_time = $data->over_time;
        $type = $data->type;
        $flag1 = $data->flag1;
        $ad_img = $data->ad_img;
        $beizhu = $data->beizhu;
        $sql2 = "UPDATE ad SET beizhu='{$beizhu}',name='{$name}',href='{$href}',start_time='{$start_time}',over_time='{$over_time}',type='{$type}',flag='{$flag1}',ad_img='{$ad_img}' WHERE id='{$ids}'";
        //更新广告
        if(flag($data)){
            $stmt2 = $conn->prepare($sql2);
            $stmt2->execute();
            echo get_msg('200','操作成功~',false,false);
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };
    }// 更新广告

    if ($id == '1018') {
        $sql1 = "SELECT * FROM ad";
        if(flag($data)){
            $stmt1 = $conn->query($sql1);
            $stmt1->execute();
            $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
            $result1 = $stmt1->fetchAll();
            if ($result1) {
                echo get_msg('200','操作成功~',false,$result1);
            } else {
                echo get_msg('400','数据异常~',false,false);
            }
        }else{
            echo get_msg('500','权限不足~','../index.html',false);
        };
    }// 输出内容

    $conn = null;
} else {
    echo "<script>\r\n    window.location.href = '../../';\r\n    </script>";
}